<?php
/**
 * Regions module
 * 
 * @author Konstantin Shamko <konstantin.shamko@gmail.com> 
 * @version 0.0.1
 * @copyright  Copyright (c) 2009 Konstantin Shamko
 * @package Locations Module
 * @subpackage Model
 * @license  New BSD License
 *
 */
class Models_Locations_Regions extends Bel_Model_Abstract {
	
	/**
	 * Describes many-to-one ralation between regions and countries
	 *
	 * @var array
	 */	
	protected $_referenceMap = array (
									'Country' => 
											array ('columns' => 'country_id',
												   'refTableClass' => 'Models_Locations_Countries',
									   			   'refColumns' => 'country_id',
												   'onDelete'=>self::CASCADE	
									   			   )													
									);
		
	/**
	 * Constructor. Sets table name & primary key
	 *
	 * @param array $array - see Zend_Db_Table_Abstract for details
	 */	
	public function __construct($array = array()) {
		$this->_name = TABLE_PREFIX . 'country_regions';
		$this->_primary = array ('country_region_id' );
		parent::__construct ( $array );
	}
	
	/**
	 * Prepares select query for getAll method. This select will return regions with countries 
	 *
	 * @return Zend_Db_Select
	 */
	protected function _selectQuery(){
		$oCountries = new Models_Locations_Countries();
		$select  = parent::_selectQuery(); 
		return $select->joinLeft(array('c'=>$oCountries->info('name')),'c.country_id='.$this->_name.'.country_id');
	}
	
	/**
	 * Returns regions by country
	 *
	 * @param int $country_id
	 * @return Zend_Db_Table_Rowset_Abstract
	 */
    public function getRegionsByCountry($country_id){
    	return $this->fetchAll("country_id='".$country_id."'",'country_region_name');
    }
    	
}